package cn.cai.service.impl;

import cn.cai.exceptionHandler.GuliException;
import cn.cai.mapper.EduVideoMapper;
import cn.cai.pojo.EduChapter;
import cn.cai.mapper.EduChapterMapper;
import cn.cai.pojo.EduVideo;
import cn.cai.pojo.vo.ChapterVo;
import cn.cai.pojo.vo.VideoVo;
import cn.cai.service.IEduChapterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author caishenao
 * @since 2021-08-17
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements IEduChapterService {

    @Autowired
    private EduVideoMapper eduVideoMapper;

    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1.根据课程id查询里面所有的章节
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(queryWrapper);
        //2.根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoMapper.selectList(eduVideoQueryWrapper);
        //创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();
        //3.遍历查询章节list集合进行封装
        //遍历查询章节list集合
        for (int i = 0; i < eduChapterList.size(); i++) {
            //每个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //eduChapter 对象值复制到ChapterVo里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapterVo放到最终list集合
            finalList.add(chapterVo);

            //创建集合，用于封装章节的小节
            List<VideoVo> videoVoList = new ArrayList<>();

            //4.遍历查询小节list集合，进行封装
            for (int m = 0; m < eduVideoList.size(); m++) {
                //得到每个小节
                EduVideo eduVideo = eduVideoList.get(m);
                //判断：小节里卖弄chapterid和章节里面id是否一样
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到小节封装集合
                    videoVoList.add(videoVo);
                }
            }
            //把封装之后小节list集合，放到章节对象里面
            chapterVo.setChilden(videoVoList);

        }


        return finalList;
    }

    /**
     * 实现删除章节信息并将章节下的所有小节（视频删除）
     * @param chapterId 要删除章节id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteAndVideo(String chapterId) {
        //查询章节id对应的所有课程小节
        QueryWrapper<EduVideo> queryWrapper  = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        eduVideoMapper.delete(queryWrapper);
        //删除课程
        baseMapper.deleteById(chapterId);
        return true;
    }

    /**
     * --TODO--
     * @param chapterId
     */
    @Override
    public void deleteOnlyChapter(String chapterId) {
        //查询课程章节id中是否有小节
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        List<EduVideo> eduVideoList = eduVideoMapper.selectList(queryWrapper);
        if (eduVideoList.size() > 0){
            throw new GuliException(20001,"请先删除章节下的小节");
        }else {
            baseMapper.deleteById(chapterId);
        }
    }
}
